package com.zhongxi.guoyuan.adapter.rewardCoin;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.GridOrderAdapter;
import com.zhongxi.guoyuan.adapter.RewardOrderAdapter;
import com.zhongxi.guoyuan.entity.rewardcoin.MirrorInfo;
import com.zhongxi.guoyuan.entity.rewardcoin.RewardCoinOrderInfo;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.CommonTool;
import com.zhongxi.guoyuan.utils.Constants;
import com.zhongxi.guoyuan.utils.DataUtil;
import com.zhongxi.guoyuan.utils.GlideRoundTransform;
import com.zhongxi.guoyuan.utils.UiUtils;
import com.zhongxi.guoyuan.view.rewardCoin.RewardCoinShoperAtivity;
import com.zhongxi.guoyuan.widget.goldshop.NoScrollGridView;
import com.zhongxi.guoyuan.widget.sliding.NoScrollListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/8/29.
 */

public class RewardAdapter extends BaseAdapter {

    private JSONObject mirrorInfo;
    private List<RewardCoinOrderInfo> rewardData;
    private Context context;
    private JSONObject shopInfo;
    private JSONObject orderCus;
    private Handler handler;
    private int mExpandedMenuPos = -1;

    public RewardAdapter(Context context, List<RewardCoinOrderInfo> rewardData, JSONObject mirrorInfo, Handler handler, JSONObject orderCus) {
        this.context = context;
        this.rewardData = rewardData;
        this.mirrorInfo = mirrorInfo;
        this.handler = handler;
        this.orderCus = orderCus;
    }

    @Override
    public int getCount() {
        if (rewardData != null && rewardData.size() > 0) {
            return rewardData.size();
        }
        return 0;
    }

    public int getmExpandedMenuPos() {
        return mExpandedMenuPos;
    }

    public void setmExpandedMenuPos(int mExpandedMenuPos) {
        this.mExpandedMenuPos = mExpandedMenuPos;
    }

    @Override
    public Object getItem(int i) {
        return rewardData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View convertView, ViewGroup viewGroup) {
        try {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(context, R.layout.item_reward_door, null);
                holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
                holder.gvList = (NoScrollGridView) convertView.findViewById(R.id.gvList);
                holder.tvOrderId = (TextView) convertView.findViewById(R.id.tvOrderId);
                holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
                holder.tvCash = (TextView) convertView.findViewById(R.id.tvCash);
                holder.tvPay = (TextView) convertView.findViewById(R.id.tvPay);
                holder.tv_delete = (TextView) convertView.findViewById(R.id.tv_delete);
                holder.tvAward = (TextView) convertView.findViewById(R.id.tvAward);
                holder.tvGift = (TextView) convertView.findViewById(R.id.tvGift);
                holder.lvList = (NoScrollListView) convertView.findViewById(R.id.lvList);
                holder.ivDown = (ImageView) convertView.findViewById(R.id.iv_down);
                holder.listMenu = (LinearLayout) convertView.findViewById(R.id.listMenu);
                holder.linearImg = (RelativeLayout) convertView.findViewById(R.id.linearImg);
                holder.linearBackground = (LinearLayout) convertView.findViewById(R.id.has_reward_background);

                holder.tvTips = (TextView) convertView.findViewById(R.id.has_reward_tips);
                holder.tvName = (TextView) convertView.findViewById(R.id.consume_reward_shopname);
                holder.tvPhone = (TextView) convertView.findViewById(R.id.consume_reward_shopphone);
                holder.tvAddress = (TextView) convertView.findViewById(R.id.consume_reward_address);
                holder.ivShop = (ImageView) convertView.findViewById(R.id.iv_shop);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }


            holder.ivDown.setImageResource(i == mExpandedMenuPos ? R.mipmap.iv_down_light : R.mipmap.iv_down);
            holder.listMenu.setVisibility(i == mExpandedMenuPos ? View.VISIBLE : View.GONE);

            if (rewardData.get(i) != null && !"".equals(rewardData.get(i))) {
                final RewardCoinOrderInfo data = rewardData.get(i);
                if (null != data.getId() && !"".equals(data.getId())) {
                    holder.tvOrderId.setText("订单：" + data.getId());
                } else {
                    holder.tvOrderId.setText("");
                }
                if (null != data.getPaytime() && !"".equals(data.getPaytime())) {
                    holder.tvTime.setText("支付：" + data.getPaytime());
                } else {
                    holder.tvTime.setText("");
                }

                if (null != data.getOrdertype() && !"".equals(data.getOrdertype())) {
                    if (data.getOrdertype().equals("2")) {
                        if (null != data.getShouldprice() && !"".equals(data.getShouldprice())) {
                            holder.tvPay.setText("金额：" + data.getShouldprice() + " 元");
                            if (DataUtil.moreThanZero(CommonTool.getWhole(DataUtil.multiplys(data.getShouldprice(), Constants.REDBEAN_RATE,4)))){

                                holder.tvAward.setText(UiUtils.getString(R.string.award_hongdou) +CommonTool.getWhole(DataUtil.multiplys(data.getShouldprice(), Constants.REDBEAN_RATE,4)) + " 红豆");
                            }else {
                                holder.tvAward.setText("");

                            }
                            holder.tvGift.setText(UiUtils.getString(R.string.goldenDesc) + data.getShouldprice() + " 元");
                        } else {
                            holder.tvPay.setText("");
                            holder.tvAward.setText("");
                            holder.tvGift.setText("");
                        }

                        if (null != data.getGoodsnum() && !"".equals(data.getGoodsnum())) {
                            holder.tvCash.setText("自提" + data.getGoodsnum() + "件");
                        } else {
                            holder.tvCash.setText("自提0件");
                        }

                        holder.linearBackground.setBackgroundColor(context.getResources().getColor(R.color.bg_time));
                        holder.tvCash.setTextColor(context.getResources().getColor(R.color.title_blue));
                        holder.ivShop.setImageResource(R.mipmap.iv_goshopping);
                        holder.tvTips.setText("商家信息:");

                        if (shopInfo != null && !"".equals(shopInfo)) {
                            JSONObject jsonObject = shopInfo.optJSONObject(rewardData.get(i).getShopid());
                            if (jsonObject != null && !"".equals(jsonObject)) {
                                String id = jsonObject.optString("id");

                                holder.tvName.setText(jsonObject.optString("name"));
                                holder.tvPhone.setText(jsonObject.optString("phone"));
                                holder.tvAddress.setText(jsonObject.optString("address"));
                            }
                        } else {
                            holder.tvName.setText("");
                            holder.tvPhone.setText("");
                            holder.tvAddress.setText("");
                        }

                    } else {
                        if (null != data.getShouldprice() && !"".equals(data.getShouldprice())) {
                            if (null != data.getPostage() && !"".equals(data.getPostage())) {
                                holder.tvPay.setText("金额：" + DataUtil.add(data.getShouldprice(), data.getPostage()) + " 元");
                            } else {
                                holder.tvPay.setText("金额：" + data.getShouldprice() + " 元");
                            }
                            if (DataUtil.moreThanZero(CommonTool.getWhole(DataUtil.multiplys(data.getShouldprice(), Constants.REDBEAN_RATE,4)))){

                                holder.tvAward.setText(UiUtils.getString(R.string.award_hongdou) + CommonTool.getWhole(DataUtil.multiplys(data.getShouldprice(), Constants.REDBEAN_RATE,4)) + " 红豆");
                            }else {
                                holder.tvAward.setText("");

                            }
                            holder.tvGift.setText(UiUtils.getString(R.string.goldenDesc) + data.getShouldprice() + " 元");
                        } else {
                            holder.tvPay.setText("");
                            holder.tvAward.setText("");
                            holder.tvGift.setText("");
                        }

                        if (null != data.getGoodsnum() && !"".equals(data.getGoodsnum())) {
                            holder.tvCash.setText("邮寄" + data.getGoodsnum() + "件");
                        } else {
                            holder.tvCash.setText("邮寄0件");
                        }
                        holder.linearBackground.setBackgroundColor(context.getResources().getColor(R.color.cart_background));
                        holder.tvCash.setTextColor(context.getResources().getColor(R.color.title_red));
                        holder.tvTips.setText("提货人信息:");
                        holder.ivShop.setImageResource(R.mipmap.iv_person_home);

                        if (orderCus != null && !"".equals(orderCus)) {
                            JSONObject jsonObject = orderCus.optJSONObject(rewardData.get(i).getId());
                            if (jsonObject != null && !"".equals(jsonObject)) {
                                String id = jsonObject.optString("id");
                                holder.tvName.setText(jsonObject.optString("consignee"));
                                holder.tvPhone.setText(jsonObject.optString("consigneemphone"));
                                holder.tvAddress.setText(jsonObject.optString("shippingaddress"));
                            }
                        } else {
                            holder.tvName.setText("");
                            holder.tvPhone.setText("");
                            holder.tvAddress.setText("");
                        }
                    }
                }

                List<String> imageList = new ArrayList<>();
                List<MirrorInfo> mirrList = new ArrayList<>();
                final JSONArray jsonArray = mirrorInfo.optJSONArray(data.getId());
                for (int j = 0; j < jsonArray.length(); j++) {
                    JSONObject jsonObject = (JSONObject) jsonArray.get(j);
                    MirrorInfo mirrorInfo = MirrorInfo.parseMatch(jsonObject);
                    mirrList.add(mirrorInfo);
                    imageList.add(mirrorInfo.getThumbnail());
                }
                holder.linearImg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        try {
                            JSONObject obj = (JSONObject) jsonArray.get(0);
                            String shopid = obj.optString("shopid");
                            Intent intent = new Intent(context, RewardCoinShoperAtivity.class);
                            intent.putExtra("shopid", shopid);
                            context.startActivity(intent);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

                if (imageList != null && imageList.size() > 0) {
                    if (imageList.size() > 1) {
                        holder.ivImg.setVisibility(View.GONE);
                        holder.gvList.setVisibility(View.VISIBLE);
                        holder.gvList.setAdapter(new GridOrderAdapter(context, imageList));
                    } else {
                        holder.ivImg.setVisibility(View.VISIBLE);
                        holder.gvList.setVisibility(View.GONE);
                        Glide.with(context).load(Url.IMG_URL + imageList.get(0)).placeholder(R.mipmap.iv_eg).into(holder.ivImg);
                    }
                }

                holder.lvList.setAdapter(new RewardOrderAdapter(context, mirrList));
            }


            holder.tv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    AlertDialog dialog = new AlertDialog.Builder(context).setTitle("您确认要删除该项目吗？")
                            .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Message msg = Message.obtain();
                                    msg.obj = i;
                                    msg.what = 0;
                                    handler.sendMessage(msg);
                                    dialog.dismiss();
                                }
                            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            }).show();
                    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(context.getResources().getColor(R.color.bg_yield));
                    dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(context.getResources().getColor(R.color.bg_yield));
                }
            });


        } catch (JSONException e) {
            e.printStackTrace();
        }
        return convertView;
    }

    public void setShopInfo(JSONObject shopInfo) {
        this.shopInfo = shopInfo;
    }

    class ViewHolder {
        private ImageView ivImg;
        private NoScrollGridView gvList;
        private TextView tvOrderId;
        private TextView tvTime;
        private TextView tvCash;
        private TextView tvPay;
        private TextView tv_delete;
        private NoScrollListView lvList;
        private TextView tvAward;
        private TextView tvGift;
        private ImageView ivDown;
        private LinearLayout listMenu;

        private LinearLayout linearBackground;
        private RelativeLayout linearImg;
        private TextView tvName;
        private TextView tvPhone;
        private TextView tvAddress;
        private TextView tvTips;

        private ImageView ivShop;
    }
}
